using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.Net.NetworkInformation;
using System.Web;
using System.Security.Principal;
namespace Test {
	class CPU {
		public static DAL loca=new DAL();
		static void MainX() {

			List<string> servers=new List<string>();
			servers.AddRange(Mixer.CombineEx("pon-wprf{0:00}-cl{1:00}","1-3,5-7","01,02"));
			//servers.AddRange(Mixer.CombineEx("pon-wprf{0:00}-cl{1:00}","3","01,02"));
			servers.AddRange(Mixer.CombineEx("pon-wpwb{0:00}-cl{1:00}","3","1,2"));
			//servers.AddRange(Mixer.CombineEx("pon-wpct{0:00}","51-75"));
			//servers.Add("pon-wpsq16-db");
			servers.AddRange(Mixer.CombineEx("pon-wpsq08-cl{0:00}","1,2"));
			servers.AddRange(Mixer.CombineEx("pon-wpsq16-cl{0:00}","1,2"));
			servers.AddRange(Mixer.CombineEx("pon-wpsq21-cl{0:00}","1-6"));

			//loca.NonQry("create table performance(id int identity,inserted datetime default getdate(),srv varchar(50),cnt float)");
			List<Thread> thds=new List<Thread>();
			List<PerformanceCounter> lst=new List<PerformanceCounter>();
			//servers.Add("wpndm08");
			//for(int i=51;i<=75;i++)
			//{
			//  //lst.Add(new PerformanceCounter("Process","% Processor Time","_Total","pon-wpct"+i));
			//  Thread th=new Thread(Add);
			//  thds.Add(th);
			//  th.Start(new object[] { lst,"pon-wpct"+i });
			//}
			//foreach(string str in Mixer.Combine("pon-wpwb0{0}-cl0{1}","1,2,3","1,2"))	
			//foreach(string str in Mixer.Combine("pon-wpsq21-db{0}","01,02,03,04,05,06,07,08,09,10,11"))
			//foreach(string str in )
			//servers.AddRange(Mixer.Combine("pon-wpmq01-cl{0}","01,02"));
			//servers.AddRange(Mixer.Combine("pon-wpwb{0}-cl{1}","01,02,03","01,02"));

			//servers.AddRange(Mixer.CombineEx("pon-wpsq21-db{0:00}","1-12"));
			//servers.AddRange(Mixer.Combine("pon-wpsq{0}-db","01,02,03,06,07,08,09,10,11,12,13,16"));
			//servers.AddRange(Mixer.Combine("pon-wpsq21-cl0{0}","2,3,4,5,6"));
			//servers.AddRange(Mixer.Combine("pon-wpsq08-cl0{0}","1,2"));
			//servers.AddRange(Mixer.Combine("pon-wpwb03-cl0{0}","1,2"));

			//servers.Add("pon-wpas10-svc");
			Stopwatch sw=Stopwatch.StartNew();
			foreach(string str in servers)
			{
				//lst.Add(new PerformanceCounter("Process","% Processor Time","_Total","pon-wpct"+i));
				Thread th=new Thread(Add);
				thds.Add(th);
				th.Start(new object[] { lst,str });
			}

			foreach(Thread thd in thds)
				thd.Join();
			Console.WriteLine(sw.Elapsed);
			//loca.NonQry("truncate table performance");
			//while(true)
			//{
			//  foreach(PerformanceCounter pc in lst)
			//  {
			//    try
			//    {
			//      float val=pc.NextValue();
			//      loca.NonQry("insert into performance(srv,cnt)values('{0}',{1})",pc.MachineName,val);
			//    }
			//    catch(Exception ex) { Console.WriteLine(ex.Message); }
			//    Thread.Sleep(20);
			//  }
			//  //Console.WriteLine(new string('-',50));
			//}
		}
		static void Add(object obj) {
			object[] arr=obj as object[];

			//List<PerformanceCounter> lst=arr[0] as List<PerformanceCounter>;
			PerformanceCounter pc=new PerformanceCounter("Processor","% Processor Time","_Total",arr[1] as string);
			//PerformanceCounter pc=new PerformanceCounter("Processor","% User Time","_Total",arr[1] as string);
			//PerformanceCounter pc=new PerformanceCounter("PhysicalDisk","Split IO/Sec","_Total",arr[1] as string);
			pc.NextValue();
			//lst.Add(pc);
			Console.WriteLine(pc.MachineName);

			while(true)
			{


				try
				{
					float val=pc.NextValue();
					loca.NonQry("insert into performance(srv,cnt)values('{0}',{1})",pc.MachineName,val);
				}
				catch(ObjectDisposedException ex)
				{
					Console.WriteLine(pc.MachineName+" - "+ex.Message);
					//pc=new PerformanceCounter("Processor","% Processor Time","_Total",arr[1] as string);

				}
				Thread.Sleep(3300);

				//Console.WriteLine(new string('-',50));
			}

		}
		static void MainC() {
			List<PerformanceCounter> lst=new List<PerformanceCounter>();
			for(int i=51;i<=75;i++)
			{
				string machine="pon-wpct"+i;
				{
					string[] instances=GetProcessInstanceNames(machine);
					foreach(string str in instances)
						lst.Add(new PerformanceCounter("Process","% Processor Time",str,machine));
				}
			}
			if(lst.Count>0)
			{
				for(int k=0;k<50;k++)
				{
					//System.Threading.Thread.Sleep(500);
					foreach(PerformanceCounter pc in lst)
					{
						float val=0;
						try
						{
							val=pc.NextValue();
						}
						catch(Exception ex) { Console.WriteLine(ex.Message); }
						if(val>3)
							Console.WriteLine(pc.MachineName+" - "+pc.InstanceName+": "+val);
					}
					Console.WriteLine(new string('-',50));
				}
			}
		}
		private static string[] GetProcessInstanceNames(string machine) {
			List<string> inst=new List<string>();
			PerformanceCounterCategory cat=new PerformanceCounterCategory("Process",machine);
			string[] instances=cat.GetInstanceNames();
			foreach(string instance in instances)
			{
				using(PerformanceCounter cnt=new PerformanceCounter("Process","ID Process",instance,machine))
					if(cnt.InstanceName.ToLower().Contains("returns"))
						inst.Add(instance);
			}
			return inst.ToArray();
		}
	}
	class PingGatherer {
		public PingGatherer(string ip) { _ip=ip; }
		string _ip;
		DAL dal=new DAL();
		public void Ping() {

			int roundtrip=1000;
			PingReply pr=new Ping().Send(_ip,roundtrip);
			if(pr.Status==IPStatus.Success)
				roundtrip=(int)pr.RoundtripTime;
			else
			{
				Console.WriteLine(_ip+"-"+pr.Status);
			}
			dal.NonQry("insert into Ping(ip,pval,perr)values('{0}',{1},{2})",_ip,roundtrip,(int)pr.Status);

		}

	}
}
